using System;
using System.Data;
using System.Data.SQLite;
using SqlSugar;

namespace SqlSugarEx.Realization
{
	public class SqliteProviderEx : SqliteProvider
	{
		private SQLiteDataAdapter _sqliteAdp;

		private DataSet _dsBind;

		public override DataTable GetDataTable(string sql, params SugarParameter[] parameters)
		{
			SQLiteConnection sQLiteConnection = new SQLiteConnection
			{
				ConnectionString = this.Connection.ConnectionString
			};
			try
			{
				SqlSugar.IDataAdapter adapter = this.GetAdapter();
				if (adapter is SqliteDataAdapter && adapter is SqliteDataAdapter)
				{
					SQLiteCommand sQLiteCommand = new SQLiteCommand(sql, sQLiteConnection);
					foreach (SugarParameter sugarParameter in parameters)
					{
						sQLiteCommand.Parameters.Add(new SQLiteParameter(sugarParameter.ParameterName, sugarParameter.Value));
					}
					this._sqliteAdp = new SQLiteDataAdapter(sQLiteCommand);
					this._sqliteAdp.MissingSchemaAction = MissingSchemaAction.AddWithKey;
					SQLiteCommandBuilder sQLiteCommandBuilder = new SQLiteCommandBuilder(this._sqliteAdp);
					this._dsBind = new DataSet();
					this._sqliteAdp.Fill(this._dsBind);
					return this._dsBind.Tables[0];
				}
				return new DataTable();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection)
				{
					sQLiteConnection.Close();
				}
			}
		}

		public bool UpdateBind()
		{
			try
			{
				this._sqliteAdp.Update(this._dsBind);
				this._sqliteAdp.Fill(this._dsBind);
				this._dsBind.AcceptChanges();
				return true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
